function quantidade(val, meta, record) {
	if(val == null) val = '';
	var input = '<input type="text" value="' + val + '" ' + 
	' class="qtde-input qtde-input-' + record.id + '" />';
    return input;
}


Ext.define('PW.view.papeis.Empresas' ,{
    extend: 'Ext.grid.GridPanel',
    alias : 'widget.infoEmpresas',
    title : 'Empresas',
    //store : 'Empresas',
    loadMask: true,
    
    columns : [{
        header: 'Sigla', 
        dataIndex: 'sigla'
    }, {
        header: 'Nome',
        dataIndex: 'nome'
    }, {
        header: 'Cotacao', 
        dataIndex: 'cotacao'
    }, {
        header: 'Quatidade', 
        renderer : quantidade,
    }, {
        header: 'Preco',
        dataIndex: 'preco'
    }, {
		xtype : 'actioncolumn',
		width : 50,
		items : [ {
			icon : 'ext-4.1.1/resources/images/16x16/edit.png',
			tooltip : 'Detalhes',
			id : 'detalhes',
			action : 'showDetalhes',
			handler: function(view, rowIndex, colIndex, item, e) {
                this.fireEvent('itemclick', this, 'sell', view, rowIndex, colIndex, item, e);
            }
		}]
	}],
	
//    dockedItems: [{
//        xtype: 'pagingtoolbar',
//        dock: 'bottom',
//        displayInfo: true,
//        items: [
//                { 
//                    xtype: 'tbseparator'
//                },
//                {
//                    xtype : 'button',
//                    text: 'Add Customer',
//                    action: 'add'
//                }
//        ]
//    }],
    
    initComponent : function() {
		Ext.apply(this, {
			itemSelector : 'div.post-wrap',
			tpl : new Ext.XTemplate('<tpl for=".">',
					'<div class="post-wrap">',
					'<h2>{title}</h2>', '<p>{content}</p>', '</div>', '</tpl>')
		});
		this.store = Ext.data.StoreManager.lookup('PW.store.Empresas');
		this.store.load();
		
		this.callParent(arguments);
		

        // Bind events that will handle editors creation
        this.on({
            viewready: this.refreshEditors,
            scope: this
        });
        this.getView().on({
        	keyPress: this.refreshEditors,
            scope: this
        });
	},


    // Method which creates editors
    refreshEditors: function() {
        var els = this.getView().getNodes();
        var actual = {};
        
        // create new editors
        for(var i = 0, ilen = els.length; i < ilen; ++i) {
            var input = Ext.fly(els[i]).query('.qtde-input')[0];
            var cls = input.className.replace(/^.*(qtde-input-[^ ]+).*$/gi, '$1'); // get id from class
            
            if (this.editors[cls] === undefined) {
                this.editors[cls] = Ext.create({
                    xtype: 'numberfield',
                    applyTo: input,
                    listeners: {
                        change: function() {
                            alert('changed');
                        }
                    }
                });
            }
            actual[cls] = true;
        }
        
        // destroy old editors eg after removing rows
        for(var j in this.editors) {
            if (actual[j] === undefined) {
                this.editors[j].destroy();
                delete this.editors[j];
            }
        }
    },
    
});